package net.robotmedia.acv.comic;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.List;

import net.robotmedia.acv.Constants;
import net.robotmedia.acv.comic.Comic.ImageState;
import net.robotmedia.acv.logic.TrackingManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.bluesky.android.utils.*;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.net.Uri;

public class BlueSkypeComic extends Comic {
	
	private List<String> orderedScreens;
	private List<String> listURLs;
	private String defaultScreen;	
	private int length = 0;
	
	public String getDefaultScreen() {
		return defaultScreen;
	}

	public void setDefaultScreen(String defaultScreen) {
		this.defaultScreen = defaultScreen;
	}
	public List<String> getOrderedScreens() {
		return orderedScreens;
	}

	public void setOrderedScreens(List<String> orderedScreens) {
		this.orderedScreens = orderedScreens;
		length = orderedScreens.size();
	}
	
	public List<String> getListURLs() {
		return listURLs;
	}

	public void setListURLs(List<String> listURLs) {
		this.listURLs = listURLs;
	}
	
	protected BlueSkypeComic(String path) {
		super(path);
		// TODO Auto-generated constructor stub
	}

	
	
	
	
	
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public int getLength() {
		// TODO Auto-generated method stub
		return length;
	}

	@Override
	public Drawable getScreen(int position) {
		// TODO Auto-generated method stub
		Drawable returnDrawable = null;
		if(position > -1 && position < orderedScreens.size()){
			String imagePathItem = orderedScreens.get(position);
			if(imagePathItem != null && imagePathItem != ""){
				File file = new File(imagePathItem);
				if(!file.exists()){
					Bitmap bitmap = DownloadImage(position, false);
					if(!file.exists()){
						returnDrawable = Drawable.createFromPath(defaultScreen);
					}else
					{
						returnDrawable = Drawable.createFromPath(file.getPath() );
					}
				}
				else{
					returnDrawable = Drawable.createFromPath(file.getPath() );	
				}
				
			}
		}
		
					
		return returnDrawable;
	}

	@Override
	public Drawable getThumbnail(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri getUri(int position) {
		// TODO Auto-generated method stub
		String filePath;
		filePath = getOrderedScreens().get(position);
		return filePath != null ? Uri.fromFile(new File(filePath)) : null;
	}

	@Override
	public void prepareScreen(int position) {
		// TODO Auto-generated method stub
		if(position > 0 && position < orderedScreens.size()){
			String imagePathItem = orderedScreens.get(position);
				if(imagePathItem != null && imagePathItem != ""){
				File file = new File(orderedScreens.get(position) );
				if(!file.exists()){
					DownloadImage(position, true);			
				}	
			}
		}
	}
	
	protected String saveBitmapToDisk(String filePath, Bitmap bitmap) {
		FileOutputStream out = null;
		try {
			File file = new File(filePath);
			out = new FileOutputStream(file);
			boolean success = bitmap.compress(CompressFormat.JPEG, Constants.COMPRESSION_QUALITY, out);
			out.close();
			if (success) {
				return file.getPath();
			}
		} catch (Exception e) {
			e.printStackTrace();
			TrackingManager.trackError("Comic.saveBitmap", e);
			if (out != null) {
				try { out.close(); } catch (Exception e1) {}
			}
		} finally {
			bitmap = null;
		}
		return null;
	}
	
	private synchronized Bitmap DownloadImage(int position, boolean recycle){		
		Bitmap bitmap = null; 
		String url = listURLs.get(position);
		File fileTemp = null;
		Bitmap bitmapOnNetwork = MangaUtility.GetImageFromURL(url);
	    if(bitmapOnNetwork != null){
	          	fileTemp = createTempFile("Cache" + position); 
	           	saveBitmapToDisk(fileTemp.getPath(), bitmapOnNetwork);
	           	BitmapFactory.decodeFile(fileTemp.getPath(), bounds);
	           	int width = bounds.outWidth;
		  		int height = bounds.outHeight;
		   		boolean landscape = height > width;
		   		int maxHeight = getMaxHeight(landscape);
		   		int maxWidth = getMaxWidth(landscape);
		    		
		   		boolean avoidResample = width <= maxWidth && height <= maxHeight;
		   		if (!avoidResample) {	    			
		   			BitmapFactory.Options options = new BitmapFactory.Options();
		   			options.inSampleSize = calculateSampleSize(width, height);
		   			bitmap =  BitmapFactory.decodeFile(fileTemp.getPath(), options);    			
		   		}else{
		   			bitmap =  BitmapFactory.decodeFile(fileTemp.getPath());   
		   		}
		   		
		   		if (bitmap != null) {
	    			final String tempFileName = orderedScreens.get(position);
	    			saveBitmapToDisk(tempFileName, bitmap);
	    			imageState.put(String.valueOf(position), ImageState.MODIFIED);
		   		}
		   		if (recycle && bitmap != null) {
		   			bitmap.recycle();
		   		}	
		   		if(fileTemp != null && fileTemp.exists()){
		   			fileTemp.delete();
		   		}
	    }
	    return bitmap;
	}
	
	
}
	










